package com.example.yogurtsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.yogurtsystem.dto.StatisticsDTO;
import com.example.yogurtsystem.entity.Statistics;
import com.example.yogurtsystem.mapper.StatisticsMapper;
import com.example.yogurtsystem.mapper.UserRecordMapper;
import com.example.yogurtsystem.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Da
 * @date 2021/08/22
 **/
@Service
public class StatisticsServiceImpl extends ServiceImpl<StatisticsMapper,Statistics> implements StatisticsService {

    @Resource
    private UserRecordMapper userRecordMapper;

    @Autowired
    private StatisticsMapper statisticsMapper;

    @Override
    public void createStatisticsByDay(String day) {
        // 删除已存在的统计对象
        QueryWrapper<Statistics> dayQueryWrapper = new QueryWrapper<>();
        dayQueryWrapper.eq("date_calculate", day);
        baseMapper.delete(dayQueryWrapper);

        // 获取当天的点餐金额和点餐人数
        BigDecimal countPriceByDay = userRecordMapper.countPriceByDay(day);
        Integer countUserByDay = userRecordMapper.countUserByDay(day);

        // 当天没有点餐数据则不统计
        if (countUserByDay == 0){
            return;
        }

        Statistics statistics = new Statistics();
        statistics.setDrinkNum(countUserByDay);
        statistics.setPriceCount(countPriceByDay);
        statistics.setDateCalculate(day);
        baseMapper.insert(statistics);

    }

    @Override
    public Map<String, Object> getChartData(String begin, String end, String type) {
        QueryWrapper<Statistics> dayQueryWrapper = new QueryWrapper<>();
        dayQueryWrapper.select(type, "date_calculate");
        dayQueryWrapper.between("date_calculate", begin, end)
            .orderByAsc("date_calculate");

        List<Statistics> dayList = baseMapper.selectList(dayQueryWrapper);
        Map<String, Object> map = new HashMap<>();
        List<Object> dataList = new ArrayList<>();
        List<String> dateList = new ArrayList<>();
        map.put("dataList", dataList);
        map.put("dateList", dateList);

        for (int i = 0; i < dayList.size(); i++) {
            Statistics daily = dayList.get(i);
            dateList.add(daily.getDateCalculate());
            switch (type) {
                case "drink_num":
                    dataList.add(daily.getDrinkNum());
                    break;
                case "price_count":
                    dataList.add(daily.getPriceCount());
                    break;
                default:
                    break;
            }
        }
        return map;
    }

    @Override
    public List<StatisticsDTO> selectStatistics(String date) {
        return userRecordMapper.selectStatistics(date);
    }
}
